from typing import *


class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        ans = c = l = 0
        mx = max(nums)
        for i, v in enumerate(nums):
            if v == mx:
                c += 1
                while c == k:
                    c -= nums[l] == mx
                    l += 1
            if l > 0:
                ans += l
        return ans


s = Solution()
print(
    s.countSubarrays(
        [
            61,
            23,
            38,
            23,
            56,
            40,
            82,
            56,
            82,
            82,
            82,
            70,
            8,
            69,
            8,
            7,
            19,
            14,
            58,
            42,
            82,
            10,
            82,
            78,
            15,
            82,
        ],
        2,
    )
)
